package com.bike.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bike.util.PageData;
import com.github.miemiedev.mybatis.paginator.domain.PageList;


 @Controller
public class BaseController {
	 protected Logger logger=LoggerFactory.getLogger(this.getClass());
	 
	 @RequestMapping("error")
	 @ResponseBody
	 public Map<String,Object> error(Exception e){
		 return getErrorMap("请登录");
	}
	 @RequestMapping("add")
	 public  String  add(String name){
		 return name;
	 }
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Map<String,Object> allException(Exception e){
		e.printStackTrace();
		return getErrorMap("请重新操作");
	}
	
	public PageData getPageResult(){
		PageData result=new PageData();
		 
		List<Object> records= new ArrayList<>();
		result.put("rows",records );
		result.put("total", ((PageList<Object>) records).getPaginator().getTotalCount());
		return result;
	}
	
	public PageData getSuccessPd(PageData rows) {
		rows.put("code", 0);
		rows.put("msg", "");
		return rows;
	}
	 public Map<String,Object> getSuccessMap(){
		 Map<String,Object> map=new HashMap<>();
		 map.put("code", 0);
		 map.put("msg", "");
		 return map;
	 }
	 public PageData getSuccessPd(){
		 PageData map=new PageData();
		 map.put("code", 0);
		 map.put("msg", "");
		 return map;
	 }
	 public PageData getErrorPd(String msg){
		 PageData map=new PageData();
		 map.put("code", 1);
		 map.put("msg", msg);
		 return map;
	 }
	 public PageData getErrorPd(PageData msg){
		 PageData map=new PageData();
		 map.put("code", 1);
		 map.put("msg", msg.getString("msg"));
		 return map;
	 }
	 public Map<String,Object> getErrorMap(String msg){
		 Map<String,Object> map=new HashMap<>();
		 map.put("code", 1);
		 map.put("msg", msg);
		 return map;
	 }
		public void responsewrite(HttpServletResponse response,String msg){
			// /设置响应的编码格式为utf-8
			response.setContentType("text/html;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			try {
				response.getWriter().write(msg);
			} catch (IOException e) {
				logger.error(e.getStackTrace().toString());
			}
		}
		 /**
		 * 获取客户端ip
		 * 
		 * @param request
		 * @return
		 */
		public String getClientIp(HttpServletRequest request) {
			String ip = request.getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			return ip;
		}
}
